package com.eastcom.providor.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.eastcom.bm.atom.ReportMapping;
import com.eastcom.client.ReportMappingBean;
import com.eastcom.providor.dao.BaseDao;

public class ReportMappingDao extends BaseDao{
	
	public List<ReportMapping> queryByDataSetId(String dataset_id) throws Exception
	{
		String hql="from ReportMapping where dataset_id=?";
		List<ReportMapping> ret=null;
		try {
			ret = this.getObjects(hql, new Object[] { dataset_id });
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return ret;
	}
	
	public ReportMapping queryByDataSetIdAndDimension(String dataset_id,String dimension) throws Exception
	{
		ReportMapping ret=null;
		String hql="from ReportMapping where dataset_id=? and dimension=?";
		try{
			ret=(ReportMapping)this.executeSelectForObject(hql, new Object[]{dataset_id,dimension});
		}catch(Exception e)
		{
			e.printStackTrace();
			throw e;
		}
		return ret;
	}
	
	public void saveReportMapping(ReportMapping reportMapping) throws Exception
	{
		try {
			this.saveObject(reportMapping);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List<ReportMappingBean> selectAllReprotMaping() throws Exception
	{
		try {
			String sql="select t1.*,t2.name_cn from qa_report_mapping t1,qa_dataset_define t2 where t1.dataset_id=t2.id";
			List<Map> results=this.getFieldsBySQL(sql, new String[]{"TASKID","DATASET_ID","HOSTIP","DIMENSION","NAME_CN"});
			List<ReportMappingBean> ret=new ArrayList<ReportMappingBean>();
			for(Map m:results)
			{
				ReportMappingBean bean=new ReportMappingBean();
				bean.setDataset_id((String)m.get("DATASET_ID"));
				bean.setDimension((String)m.get("DIMENSION"));
				bean.setHostIp((String)m.get("HOSTIP"));
				bean.setName_cn((String)m.get("NAME_CN"));
				bean.setTaskid((String)m.get("TASKID"));
				ret.add(bean);
			}
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public void removeReportMapping(String taskid,String hostip) throws Exception
	{
		String hql="delete from ReportMapping where taskid=? and hostip=?";
		try {
			this.deleteObjects(hql, new Object[]{taskid,hostip});
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List<String> selectAllDimensionsByDatasetId(String dataset_id) throws Exception
	{
		String hql="select distinct(dimension) from ReportMapping where dataset_id=?";
		List<String> ret=null;
		try {
			ret = (List<String>) this.getObjects(hql,new Object[] { dataset_id });
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return ret;
	}
}
